package v1

import (
	"net/http"
	"toy-car/models"
	"toy-car/pkg/e"
	"toy-car/service"

	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

func AutocodePackageAdd(c *gin.Context) {
	var registerData models.SysAutoCode

	_ = c.ShouldBindJSON(&registerData)

	// post参数检验
	valid := validation.Validation{}
	ok, _ := valid.Valid(&registerData)
	data := make(map[string]interface{})
	code := e.SUCCESS
	if !ok {
		code = e.INVALID_PARAMS
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  e.GetMSG(code),
			"data": registerData,
		})
		return
	}

	userRet, err := models.PackageAdd(registerData)
	if err != nil {
		code = e.INVALID_PARAMS
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  err.Error(),
			"data": data,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": userRet,
	})
}

// @Tags SysDictionaryDetail
// @Summary 分页获取SysDictionaryDetail列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysDictionaryDetailSearch true "页码, 每页大小, 搜索条件"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sysDictionaryDetail/getSysDictionaryDetailList [get]
func AutocodePackageList(c *gin.Context) {

	code := e.SUCCESS
	data := make(map[string]interface{})
	list, err := models.GetSysAutoCodeList()

	if err != nil {
		code = e.ERROR
	}
	data["data"] = list

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}

func AutocodeDelete(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	var a models.SysAutoCode
	_ = c.ShouldBindJSON(&a)

	a.ID = id
	code := e.SUCCESS

	err := models.DeleteAutocode(a)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": true,
	})
}

func GetDB(c *gin.Context) {
	code := e.SUCCESS
	dbs, err := models.GetDB()
	if err != nil {
		code = e.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": dbs,
	})
}

func GetTable(c *gin.Context) {
	dbName := c.DefaultQuery("dbName", "blog")
	code := e.SUCCESS
	dbs, err := models.GetTables(dbName)
	if err != nil {
		code = e.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": dbs,
	})
}

func GetColumns(c *gin.Context) {
	dbName := c.DefaultQuery("dbName", "blog")
	tableName := "sys_base_menus"
	code := e.SUCCESS
	dbs, err := models.GetTableColumns(dbName, tableName)
	if err != nil {
		code = e.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": dbs,
	})
}

var caser = cases.Title(language.English)

func PreviewTemp(c *gin.Context) {
	var a models.AutoCodeStruct
	_ = c.ShouldBindJSON(&a)

	a.Pretreatment() // 处理go关键字
	a.PackageT = caser.String(a.Package)
	sev := &service.AutoService{}
	autoCode, err := sev.PreviewTemp(a)
	// if err != nil {
	// 	global.GVA_LOG.Error("预览失败!", zap.Error(err))
	// 	response.FailWithMessage("预览失败", c)
	// } else {
	// 	response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
	// }

	code := e.SUCCESS
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": autoCode,
	})
}
